package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ProcurementTypeListBeen;
import cn.xfdzx.android.apps.shop.bean.ProcurementTypeSubmitBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseEditBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitEditBeen;
import cn.xfdzx.android.apps.shop.contract.ProcurementConfimListContract;
import cn.xfdzx.android.apps.shop.contract.ProcurementTypeListContract;
import cn.xfdzx.android.apps.shop.model.ProcurementConfimListModel;
import cn.xfdzx.android.apps.shop.model.ProcurementTypeListModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class ProcurementConfimListPresenter extends BasePresenter<ProcurementConfimListContract.View> implements ProcurementConfimListContract.Presenter {

    private ProcurementConfimListContract.Model model;

    public ProcurementConfimListPresenter() {
        model = new ProcurementConfimListModel();
    }


    @Override
    public void purchaseEdit(PurchaseSubmitEditBeen body) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.purchaseEdit(body)
                .compose(RxScheduler.<PurchaseSubmitBeen>Flo_io_main())
                .as(mView.<PurchaseSubmitBeen>bindAutoDispose())
                .subscribe(new Consumer<PurchaseSubmitBeen>() {
                    @Override
                    public void accept(PurchaseSubmitBeen bean) throws Exception {
                        mView.onPurchaseEditSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void purchaseDelete(String purchase_sn) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.purchaseDelete(purchase_sn)
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onPurchaseDeleteSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
